#!/bin/bash
###
 # @Author: dvlproad dvlproad@163.com
 # @Date: 2023-02-24 07:08:18
 # @LastEditors: dvlproad dvlproad@163.com
 # @LastEditTime: 2023-02-27 22:34:49
 # @FilePath: /example_packing_info/all_packing_info.sh
 # @Description: 获取当前分支所包含的分支及其分支信息。（如果企业微信等通知中不需要显示分支所包含的其他分支信息，则只需本脚本中调用执行 brances_info_log.sh 时候的请求参数不设置 "Notification" ，使其即使去获取也无值即可。）
### 
:<<!
获取当前分支所包含的分支及其分支信息

如果企业微信等通知中不需要显示分支所包含的其他分支信息，则只需本脚本中调用执行 brances_info_log.sh 时候的请求参数不设置 "Notification" ，使其即使去获取也无值即可。

!
User=`whoami`

while [ -n "$1" ]
do
    case "$1" in
#        -productWS|--product-work-space) PRODUCT_WORKSPACE=$2; shift 2;; # 项目的根目录，避免通过git命令获取当前分支的时候是获取其他git工程的分支
        -resultSaveToJsonF|--result-save-to-json-file-path) RESULT_SALE_TO_JSON_FILE_PATH=$2; shift 2;; # 为简化换行符的保真(而不是显示成换行,导致后面计算数组个数麻烦),将结果保存在的JSON文件
        --) break ;;
        *) echo $1,$2; break ;;
    esac
done


JQ_EXEC=`which jq`

exit_script() { # 退出脚本的方法，省去当某个步骤失败后，还去继续多余的执行其他操作
    exit 1
}


# 定义颜色常量
NC='\033[0m' # No Color
RED='\033[31m'
GREEN='\033[32m'
YELLOW='\033[33m'
BLUE='\033[34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'

function logAppSuccessMsg() {
    printf "${GREEN}----------------  $1  ----------------${GREEN}\n${NC}"
    # echo "----------------------------.feature_brances----------------------------"
}

function logAppProcedureMsg() {
    printf "${PURPLE}$1${PURPLE}\n${NC}"
    # echo "----------------------------.feature_brances----------------------------"
}


function debug_log() {
    if [ "${isRelease}" == true ]; then
        echo "$1"
    fi
}

#if [ $# != 5 ]; then
#    echo "当前参数个数$#，必须为5个，请设置更新说明"
#    exit 1
#fi


# 当前【shell脚本】的工作目录
# $PWD代表获取当前路径，当cd后，$PWD也会跟着更新到新的cd路径。这个和在终端操作是一样的道理的
CurrentDIR_Script_Absolute="$( cd "$( dirname "$0" )" && pwd )"
bulidScriptApp_dir_Absolute=${CurrentDIR_Script_Absolute}
echo "bulidScriptApp_dir_Absolute=${bulidScriptApp_dir_Absolute}"

#if [ -d "${PRODUCT_WORKSPACE}" ]; then
#    echo "❌Error:您的项目根目录未填写或者不存在，请检查(为了避免通过git命令获取当前分支的时候是获取其他git工程的分支)。PRODUCT_WORKSPACE=${PRODUCT_WORKSPACE}！"
#    exit 1
#fi
PRODUCT_WORKSPACE=${bulidScriptApp_dir_Absolute%/*}
echo "✅项目PRODUCT_WORKSPACE=${PRODUCT_WORKSPACE}"
cd ${PRODUCT_WORKSPACE} # 📢非常重要:为了避免通过git命令获取当前分支的时候是获取其他git工程的分支)


# 是否是使用从git上拉取的公共脚本项目(在其他项目中会这样引入本git过程)
function commonScriptProductIsFromGitclone() {
    if [ "$1" == "true" ]; then
        BUILD_SCRIPT_PRODUCT_DIR_NAME="buildScriptSource"
        BUILD_SCRIPT_PRODUCT_DIR_PATH="${PRODUCT_WORKSPACE}/${BUILD_SCRIPT_PRODUCT_DIR_NAME}"
        if [ -d "${BUILD_SCRIPT_PRODUCT_DIR_PATH}" ]; then
            rm -rf "${BUILD_SCRIPT_PRODUCT_DIR_PATH}"
            if [ $? != 0 ]; then
                echo "❌Error:rm 操作失败，请检查！"
                exit 1
            fi
        fi
        git clone -b 0.2.1 https://gitee.com/dvlpCI/AutoPackage-CommitInfo "${BUILD_SCRIPT_PRODUCT_DIR_NAME}"
        if [ $? != 0 ]; then
            echo "❌Error:git 操作失败，请检查！"
            exit 1
        fi
    else
        BUILD_SCRIPT_PRODUCT_DIR_PATH="${PRODUCT_WORKSPACE}"
    fi
}
commonScriptProductIsFromGitclone "false"
bulidScriptCommon_dir_Absolute="${BUILD_SCRIPT_PRODUCT_DIR_PATH}/bulidScriptCommon"
echo "bulidScriptCommon_dir_Absolute=${bulidScriptCommon_dir_Absolute}"
# cd ${bulidScriptApp_dir_Absolute}

qscript_path_get_filepath="${bulidScriptCommon_dir_Absolute}/qscript_path_get.sh"
qbase_update_json_file_singleString_script_path="$(sh ${qscript_path_get_filepath} qbase update_json_file_singleString)"

Environment_FILE_PATH="${bulidScriptApp_dir_Absolute}/app_info.json"


#  如果是其他项目，要单独测试本脚本的时候，需要一些前提条件，这里我们使用下面方法来补充测试时候的前提条件
function testThisScriptForOtherProduct() {
    History_Versions_FILE_PATH="${bulidScriptApp_dir_Absolute}/app_history_version.json"
    if [ ! -f "${History_Versions_FILE_PATH}" ];then
        echo "❌Error:您的History_Versions_FILE_PATH=${History_Versions_FILE_PATH}文件不存在，请检查！"
        exit 1
    fi
    sh ${qbase_update_json_file_singleString_script_path} -jsonF "${Environment_FILE_PATH}" -k "historyVersionJsonFile" -v "${History_Versions_FILE_PATH}"
    if [ $? != 0 ]; then
        echo "❌Error:为了测试本脚本，您的 historyVersionJsonFile 值History_Versions_FILE_PATH=${History_Versions_FILE_PATH}存储失败，请检查！"
        exit 1
    fi

    if [ -z "${RESULT_SALE_TO_JSON_FILE_PATH}" ]; then
        RESULT_SALE_TO_JSON_FILE_PATH="${bulidScriptApp_dir_Absolute}/app_branch_info.json"
        sh ${qbase_update_json_file_singleString_script_path} -jsonF "${Environment_FILE_PATH}" -k "branchsResultJsonFile" -v "${RESULT_SALE_TO_JSON_FILE_PATH}"
        if [ $? != 0 ]; then
            echo "❌Error:为了测试本脚本，您的 branchsResultJsonFile RESULT_SALE_TO_JSON_FILE_PATH=${RESULT_SALE_TO_JSON_FILE_PATH}存储失败，请检查！"
            exit 1
        fi
    fi
}
testThisScriptForOtherProduct

# 获取当前分支所包含的分支及其分支信息
function getBranchDetailInfo() {
    REBASE_BRANCH="origin/master"
    App_Feature_Brances_Directory_PATH="${bulidScriptApp_dir_Absolute}/featureBrances"
    Develop_Branchs_FILE_PATH="${bulidScriptApp_dir_Absolute}/app_branch_info.json"
    ignoreAddJsonFileNames=("dev_demo.json")
    ignoreCheckBranchNameArray=(master develop dev_publish_out dev_publish_in dev_all)
    logAppProcedureMsg "正在执行命令(获取当前分支所包含的分支及其分支信息)：《 ${BLUE}sh ${bulidScriptCommon_dir_Absolute}/brances_info/all_branch_info_deal.sh -envInfoF \"${Environment_FILE_PATH}\" -rebaseBranch \"${REBASE_BRANCH}\" -appBranchsDir \"${App_Feature_Brances_Directory_PATH}\" -branchInfoF \"${Develop_Branchs_FILE_PATH}\" -ignoreAddJsonFileNames \"${ignoreAddJsonFileNames}\" -ignoreCheckBranchs \"${ignoreCheckBranchNameArray[*]}\" -branchResultJsonF \"${TEST_DATA_RESULT_FILE_PATH}\" ${PURPLE}》"
    sh ${bulidScriptCommon_dir_Absolute}/brances_info/all_branch_info_deal.sh -envInfoF "${Environment_FILE_PATH}" -rebaseBranch "${REBASE_BRANCH}" -appBranchsDir "${App_Feature_Brances_Directory_PATH}" -branchInfoF "${Develop_Branchs_FILE_PATH}" -ignoreAddJsonFileNames "${ignoreAddJsonFileNames}" -ignoreCheckBranchs "${ignoreCheckBranchNameArray[*]}" -branchResultJsonF "${TEST_DATA_RESULT_FILE_PATH}"
    if [ $? != 0 ]; then
        return 1
    fi

    # 如果企业微信等通知中不需要显示分支所包含的其他分支信息，则只需本脚本中调用执行 brances_info_log.sh 时候的请求参数不设置 "Notification" ，使其即使去获取也无值即可。
    REQUEST_FOR_LOG_TYPE_ARRAY=("Notification" "Pgyer")
    logAppProcedureMsg "正在执行命令(获取执行环境中指定为文件的指定信息类型(Pgyer升级信息\Notification通知信息))：\
    《 ${BLUE}sh ${bulidScriptCommon_dir_Absolute}/brances_info/brances_info_log/brances_info_log.sh -envInfoF \"${Environment_FILE_PATH}\" --request-for-log-types \"${REQUEST_FOR_LOG_TYPE_ARRAY[*]}\" -comScriptHomeDir \"${bulidScriptCommon_dir_Absolute}\" -resultSaveToJsonF \"${RESULT_SALE_TO_JSON_FILE_PATH}\" ${PURPLE}》"
    sh ${bulidScriptCommon_dir_Absolute}/brances_info/brances_info_log/brances_info_log.sh -envInfoF "${Environment_FILE_PATH}" --request-for-log-types "${REQUEST_FOR_LOG_TYPE_ARRAY[*]}" -comScriptHomeDir "${bulidScriptCommon_dir_Absolute}" -resultSaveToJsonF "${RESULT_SALE_TO_JSON_FILE_PATH}"
    if [ $? != 0 ]; then
        return 1
    fi
}
getBranchDetailInfo
if [ $? != 0 ]; then
    exit 1
fi

echo "分支信息获取成功后，现在正在将 ${Develop_Branchs_FILE_PATH} 移到项目中，作为项目中的文件使用，用于在开发工具中显示分支信息"
Develop_Branchs_FILE_Name=${Develop_Branchs_FILE_PATH##*/}
sh ${bulidScriptCommon_dir_Absolute}/app_info_move_in.sh -sf "${Develop_Branchs_FILE_PATH}" -tf "${PRODUCT_WORKSPACE}/flutter_updateversion_kit/assets/data/${Develop_Branchs_FILE_Name}"
if [ $? != 0 ]; then
    exit 1
fi
echo "获取并检查分支的详细信息成功，继续执行(分支路径：Develop_Branchs_FILE_PATH= ${Develop_Branchs_FILE_PATH})"


